import pymysql
import logging as log


def get_database_connect(**kwargs):
    database_name = kwargs.get("database_name")
    host = kwargs.get("host")
    username = kwargs.get("username")
    password = kwargs.get("password")
    port = kwargs.get("port")

    connect_db = pymysql.connect(host=host, port=port, user=username, passwd=password, db=database_name, charset='utf8')
    return connect_db
    # MySQLDatabase(db=database_name, host=host,
    #                  user=username,
    #                  pwd=password,
    #                  port=port,
    #                  charset='utf8')


def sql_execute_without_close(sql_to_execute, mysql_config_dict):
    database_connect = get_database_connect(**mysql_config_dict)
    cursor = database_connect.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute(sql_to_execute, )
    log.info("Now execute sql({sql_to_execute}) on : {host}:{port} ".format(sql_to_execute=sql_to_execute,
                                                                            host=mysql_config_dict.get('host'),
                                                                            port=mysql_config_dict.get('port')
                                                                            ))
    # database_connect.close()
    return database_connect


def sql_execute(sql_to_execute, mysql_config_dict):
    database_connect = get_database_connect(**mysql_config_dict)
    cursor = database_connect.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute(sql_to_execute, )
    log.info("Now execute sql({sql_to_execute}) on : {host}:{port} ".format(sql_to_execute=sql_to_execute,
                                                                            host=mysql_config_dict.get('host'),
                                                                            port=mysql_config_dict.get('port')
                                                                            ))
    execute_data = cursor.fetchall()
    log.info(
        "Result of sql({sql_to_execute}):{result}".format(sql_to_execute=sql_to_execute, result=(str(execute_data))))
    database_connect.close()
    return execute_data


if __name__ == "__main__":
    # main()
    sql = """select * from  runtime_mysql_servers ;"""
    mysql_config_dict1 = {"database_name": None, "host": "10.200.11.25", "port": 8101, "password": "oW2c67^00wg3rIsF",
                         "username": "cluster"}
    data = sql_execute(sql_to_execute=sql, mysql_config_dict=mysql_config_dict1)
    print(data)
